package cn.edu.neusoft.zw725.foodorder.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import cn.edu.neusoft.zw725.foodorder.R;
import cn.edu.neusoft.zw725.foodorder.bean.RegBean;
import cn.edu.neusoft.zw725.foodorder.listener.RegListener;
import cn.edu.neusoft.zw725.foodorder.model.RegModel;

public class RegActivity extends AppCompatActivity {
    private EditText ed_reguser;
    private EditText ed_regpass;
    private EditText ed_mobilenum;
    private EditText ed_address;
    private EditText ed_comment;
    private Button btn_rreg;
    private RegListener regListener = new RegListener() {
        @Override
        public void onResponse(RegBean regBean) {
            if(regBean.getSuccess().equals("0")){
                Toast.makeText(RegActivity.this,"注册失败",Toast.LENGTH_SHORT).show();
            }
            else {
                Toast.makeText(RegActivity.this,"注册成功",Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(RegActivity.this,LoginActivity.class);
                startActivity(intent);
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(RegActivity.this,"注册失败",Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reg);

        ed_reguser = (EditText)findViewById(R.id.ed_reguser);
        ed_regpass = (EditText)findViewById(R.id.ed_regpass);
        ed_mobilenum = (EditText)findViewById(R.id.ed_mobilenum);
        ed_address = (EditText)findViewById(R.id.ed_address);
        ed_comment = (EditText)findViewById(R.id.ed_comment);
        btn_rreg = (Button)findViewById(R.id.btn_rreg);

        btn_rreg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(ed_reguser.getText().toString().trim().equals("")||ed_regpass.getText().toString().trim().equals("")||
                        ed_mobilenum.getText().toString().trim().equals("")||ed_address.getText().toString().trim().equals("")){
                    Toast.makeText(RegActivity.this,"用户名不能为空",Toast.LENGTH_SHORT).show();
                }
                else {
                    String username = ed_reguser.getText().toString();
                    String userpass = ed_regpass.getText().toString();
                    String mobilenum = ed_mobilenum.getText().toString();
                    String address = ed_address.getText().toString();
                    String comment = ed_comment.getText().toString();

                    RegModel regModel = new RegModel();
                    regModel.getRegBean(username,userpass,mobilenum,address,comment,regListener);
                }
            }
        });
    }
}
